//
//  MTWalletViewController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/17.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTWalletViewController.h"
#import "MTWalletListViewCell.h"
#import "MTWalletInfo.h"
#import "MTCoinModel.h"
#import "MTSetWalletPasswordController.h"
#import "MTValidationCodeController.h"
#import "MTWalletApiManager.h"
@interface MTWalletViewController ()
@property(nonatomic,strong) NSMutableArray *coins;

@end

@implementation MTWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"钱包";
    self.tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
    [self.tableView registerNib:[UINib nibWithNibName:@"MTWalletListViewCell" bundle:nil] forCellReuseIdentifier:@"MTWalletListViewCellID"];
    self.tableView.rowHeight = 236;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[MTWalletApiManager manager] getUserWalletInfoWithcomplation:^(MTWalletInfo *walletInfo, MTError *error) {
        if(error == nil)
        {
            if(![MTWalletApiManager manager].walletInfo.setPassword)
            {
                [[MTWalletApiManager manager] pushSetPasswordVcFromVc:self];
            }
            [[MTWalletApiManager manager] getUserCoinsWithcomplation:^(NSMutableArray<MTCoinModel *> *coins, MTError *error) {
                if(error == nil)
                {
                    [self.tableView reloadData];
                }
            }];
        }else
        {
            [MBProgressHUD showError:error.errorDescription];
        }
    }];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [MTWalletApiManager manager].coins.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTWalletListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTWalletListViewCellID"];
    cell.coinModel = [MTWalletApiManager manager].coins[indexPath.row];
    cell.indexPath = indexPath;
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
